(function ($) {
    $.fn.extend({
        sideBarScroll: function () {
            var $this = $(this), $page_sidebar_content = $(".page-sidebar-content", this), $page_sidebar_scroll = $this.find(".page-sidebar-scroll");
            var $this_height = $this.height();
            var $page_sidebar_content_height = $page_sidebar_content.height();
            var scroll_controller = function (e) {
                var return_ = false;
                var psc_position = $page_sidebar_content.position();
                if (e.deltaY == "-1") {//向下滚动
                    var top_ = psc_position.top - 50;
                    $this_height = $this.outerHeight();
                    $page_sidebar_content_height = $page_sidebar_content.outerHeight();
                    if (top_ < $this_height - $page_sidebar_content_height) {
                        top_ = $this_height - $page_sidebar_content_height;
                        if (top_ > 0) {
                            top_ = 0;
                        }
                        return_ = true;
                    }
                    $page_sidebar_content.css({"top": top_});
                } else if (e.deltaY == "1") {//向上滚动
                    var top_ = psc_position.top + 50;
                    if (top_ > 0) {
                        top_ = 0;
                        return_ = true;
                    }
                    $page_sidebar_content.css({"top": top_});
                }
                $page_sidebar_scroll_draggable_reposition();
                return return_;
            }
            $this.mousewheel(scroll_controller);
            var $page_sidebar_scroll_draggable_drag = function (e, u) {
                var $page_sidebar_scroll_top = $page_sidebar_scroll.position().top;
                var $page_sidebar_scroll_top_bfh = $page_sidebar_scroll_top / ($this.height() - $page_sidebar_scroll.height());
                var $mv_page_sidebar_content_top = ($this_height - $page_sidebar_content_height) * $page_sidebar_scroll_top_bfh;
                $page_sidebar_content.css({"top": $mv_page_sidebar_content_top});
            };
            var $page_sidebar_scroll_draggable_reposition = function () {
                var $page_sidebar_content_top_bfh = ($page_sidebar_content.position().top) / ($this_height - $page_sidebar_content_height);
                var $mv_$page_sidebar_scroll_top = ($this_height - $page_sidebar_scroll.height()) * $page_sidebar_content_top_bfh;
                $page_sidebar_scroll.css({"top": $mv_$page_sidebar_scroll_top});
            };
            $page_sidebar_scroll.draggable({
                axis: "y",
                scroll: false,
                containment: "parent",
                start: function (e, u) {
                    $this_height = $this.height();
                    $page_sidebar_content_height = $page_sidebar_content.height();
                    $page_sidebar_scroll.draggable("option", "containment", [0, 0, 0, $this.height() - $page_sidebar_scroll.height() + $this.position().top]);
                },
                drag: function (e, u) {
                    return $page_sidebar_scroll_draggable_drag(e, u);
                },
                stop: function (e, u) {
                    $this_height = $this.height();
                    $page_sidebar_content_height = $page_sidebar_content.height();
                    $page_sidebar_scroll_draggable_drag(e, u);
                }
            }).mousedown(function () {
                var $this_position = $this.position();
                $page_sidebar_scroll.draggable("option", "containment", [0, $this_position.top, 0, $this.height() - $page_sidebar_scroll.height() + $this_position.top]);
                $page_sidebar_scroll.addClass("page-sidebar-scroll-draggable");
            }).mouseup(function () {
                $page_sidebar_scroll.removeClass("page-sidebar-scroll-draggable");
            });
            $(document).mouseup(function () {
                $page_sidebar_scroll.removeClass("page-sidebar-scroll-draggable");
            });

            var sidebarShow = function () {
                ///////////////////////////////////////////////
                var t001 = setTimeout(function () {
                    $page_sidebar_content_height = $page_sidebar_content.outerHeight();
                    var sidebar_width = $this.width();
                    if ($this.is(':hidden')) {
                        $page_sidebar_scroll.removeClass("page-sidebar-scroll-display");
                    } else {
                        $page_sidebar_scroll_draggable_reposition();
                        //需要出现滚动条的时候才能显示，不需要出现滚定条的时候需要隐藏
                        $this_height = $this.outerHeight();
                        if (sidebar_width > 100 && $page_sidebar_content_height > $this_height) {
                            $page_sidebar_scroll.addClass("page-sidebar-scroll-display");
                            $page_sidebar_scroll.css("height", (($this_height / $page_sidebar_content_height) * 100).toFixed(4) + "%");
                        } else {
                            $page_sidebar_scroll.removeClass("page-sidebar-scroll-display");
                        }
                    }
                    clearTimeout(t001);
                }, 300);
            }
            sidebarShow();
            $("div[id='sidebar-collapse']").click(sidebarShow);
            $(".menu-dropdown", $this).click(sidebarShow);
            $(window).resize(sidebarShow);
            $this.click(sidebarShow);
        }
    });
})(jQuery);